package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.qf.dao.UserMapper;
import com.qf.pojo.User;
import com.qf.pojo.common.ParmeCode;
import com.qf.pojo.common.RabbitMQDic;
import com.qf.pojo.common.RedisDic;
import com.qf.pojo.response.BaseResult;
import com.qf.service.UserService;
import com.qf.utils.CookieUtils;
import com.qf.utils.JWTUtils;
import com.qf.utils.RedisUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private CookieUtils cookieUtils;

    @Override
    public BaseResult sendMail(String email) {
        BaseResult result = new BaseResult();
        rabbitTemplate.convertAndSend("", RabbitMQDic.SENDMAIL_QUEUE, email);
        result.setCode(ParmeCode.SENDMAIL_SUCCESS);
        return result;
    }


    @Override
    public BaseResult loginOrRegistry(Map map) {
        String email =(String) map.get("email");
        String code =(String) map.get("code");
        BaseResult result = new BaseResult();
        String redisCode =(String) redisUtils.hget(RedisDic.REDIS_EMAIL_KEY, email);
        if (redisCode == null){
            result.setCode(300);
            result.setMsg("请检查验证码是否正确发送或验证码已过期，请重新发送！");
            return result;
        }else if (redisCode.equals(code)){
            User byEmail = userMapper.findByEmail(email);
            if (byEmail == null) {
//            为空代表注册
                String name = "学员" + new SimpleDateFormat("MM-dd").format(new Date());
                Integer integer = userMapper.insertUser(email,name);
                if (integer != 0){
                    User byEmail1 = userMapper.findByEmail(email);
                    String token = jwtUtils.token(JSONObject.toJSONString(byEmail1));
                    result.setData(token);
                    result.setCode(400);
                    result.setMsg("注册成功");
                    return result;
                }else{
                    result.setCode(401);
                    result.setMsg("注册失败");
                    return result;
                }
            }else{
                String token = jwtUtils.token(JSONObject.toJSONString(byEmail));
                result.setData(token);
                result.setCode(200);
                result.setMsg("登陆成功");
                return result;
            }
        }else{
            result.setCode(500);
            result.setMsg("验证码错误");
            return result;
        }
    }

//    通过密码进行登录

    @Override
    public BaseResult loginByPassword(Map map) {
        String email =(String) map.get("email");
        String password =(String) map.get("password");
        BaseResult baseResult = new BaseResult();

        User byEmailAndPassword = userMapper.findByEmailAndPassword(email, password);
        if (byEmailAndPassword == null) {
            baseResult.setCode(300);
            baseResult.setMsg("密码或账号输入错误！");
            return baseResult;
        }else {
            String token = jwtUtils.token(JSONObject.toJSONString(byEmailAndPassword));
            baseResult.setCode(200);
            baseResult.setData(token);
            baseResult.setMsg("登陆成功");
            return baseResult;
        }

    }


//    解析个人信息

    @Override
    public BaseResult findMyInfo(HttpServletRequest request) {
        System.out.println(request.getCookies());
        BaseResult result = new BaseResult();
        String token=cookieUtils.getToken(request.getCookies());

        if (token == null) {
            result.setCode(300);
            return result;
        }else{
            String verify = jwtUtils.verify(token);
            if (verify == null || verify.length() == 0) {
                result.setCode(301);
                return result;
            }else {
                User user = JSONObject.parseObject(verify, User.class);
                result.setCode(200);
                result.setData(user);
                return result;
            }

        }
    }
}
